﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TaskManager.Mvc.Mobile.Extensions;
using TaskManager.Data;

using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using Newtonsoft.Json;

namespace TaskManager.Mvc.Mobile.Controllers
{
    public class MobileController : Controller
    {
        /// <summary>
        /// Get all the sprints.
        /// </summary>
        /// <returns></returns>
        public JsonpResult Sprints()
        {
            IList<Sprint> sprints = null;

            using (IUnityContainer container = new UnityContainer())
            {
                container.LoadConfiguration();
                ISprintRepository sprintRepository = container.Resolve<ISprintRepository>();
                sprints = sprintRepository.GetSprints();
            }
            return new JsonpResult(
                new
                {
                    sprints = sprints.Select(s =>
                        new
                        {
                            SprintId = s.SprintId,
                            StartDate = s.StartDate,
                            EndDate = s.EndDate
                        }
                    )
                }
            );
        }

        /// <summary>
        /// Get all the developers.
        /// </summary>
        /// <returns></returns>
        public JsonpResult Developers()
        {
            IList<Developer> developers = null;

            using (IUnityContainer container = new UnityContainer())
            {
                container.LoadConfiguration();
                ISprintRepository sprintRepository = container.Resolve<ISprintRepository>();
                developers = sprintRepository.GetDevelopers();
            }
            return new JsonpResult(
                new 
                { 
                    developers = developers.Select(d => new
                    {
                        DeveloperId = d.DeveloperId,
                        FirstName = d.FirstName,
                        LastName = d.LastName,
                        Name = d.Name,
                        Title = d.Title
                    })
                }
            );
        }

        public JsonpResult SaveDeveloper(string data)
        {
            Developer developer = JsonConvert.DeserializeObject<Developer>(data);

            using (IUnityContainer container = new UnityContainer())
            {
                container.LoadConfiguration();
                ISprintRepository sprintRepository = container.Resolve<ISprintRepository>();
                sprintRepository.SaveDeveloper(developer);
            }
            return new JsonpResult(new { result = string.Empty });
        }

        /// <summary>
        /// Get the tasks for a sprint.
        /// </summary>
        /// <param name="id">Id of the sprint.</param>
        public JsonpResult SprintTasks(int id)
        {
            Sprint sprint = null;

            using (IUnityContainer container = new UnityContainer())
            {
                container.LoadConfiguration();
                ISprintRepository sprintRepository = container.Resolve<ISprintRepository>();
                sprint = sprintRepository.GetSprints().Where(s => s.SprintId == id).First();
            }
            return new JsonpResult(
                new
                {
                    tasks = sprint.GetTasks().Select(t => new
                    {
                        TaskId = t.TaskId,
                        SprintId = t.SprintId,
                        TypeOfTask = t.TypeOfTask,
                        EstimatedHours = t.EstimatedHours,
                        Description = t.Description,
                        AssignedToId = t.AssignedToId,
                        CompletedDate = t.CompletedDate
                    })
                }
            );
        }

        /// <summary>
        /// Save a sprint.
        /// </summary>
        /// <param name="data">JSON stringified representation of a sprint.</param>
        /// <returns>string.empty if successful, else an error message.</returns>
        public JsonpResult SaveSprint(string data)
        {
            Sprint sprint = JsonConvert.DeserializeObject<Sprint>(data);

            string result = string.Empty;

            using (IUnityContainer container = new UnityContainer())
            {
                container.LoadConfiguration();
                ISprintRepository sprintRepository = container.Resolve<ISprintRepository>();
                result = sprintRepository.SaveSprint(sprint);
                sprint = (result.Length == 0
                    ? sprint
                    : sprintRepository.GetSprints().Where(s => s.SprintId == sprint.SprintId).First());
            }
            return new JsonpResult(
                new {
                    result = result,
                    sprint = sprint
                });
        }

        public JsonpResult SaveTask(string data)
        {
            Task task = JsonConvert.DeserializeObject<Task>(data);

            using (IUnityContainer container = new UnityContainer())
            {
                container.LoadConfiguration();
                ISprintRepository sprintRepository = container.Resolve<ISprintRepository>();
                sprintRepository.SaveTask(task, false);
            }
            return new JsonpResult(new { result = string.Empty });
        }
    }
}
